// pages/mydiscussion/mydiscussion.ts
Page({

    /**
     * 页面的初始数据
     * */
    data: {
        navHeight: 0,
        menuButtonInfo: {
            bottom: 0,
            height: 0,
            top: 0,
            left: 0,
            right: 0,
            width: 0,
        },
        mydiscussion: [{}],
        //查看详情
        open_index: -1,
        info: {
            img: '',
            name: '',
            phone: '',
            sex: 1,
            openid: ''
        },
        url: '',
        login: false,
        //触底标志
        again: false,
        page:1
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        var app = getApp();
       /* */
        //广告
        this.setData({
            // url: 'http://192.168.3.190:7001/'
            url: app.globalData.url,
            info: app.globalData.info,
        })
        this.setData({
            //获取胶囊一块的宽高
            menuButtonInfo: wx.getMenuButtonBoundingClientRect()
        })
        // console.log(this.data.menuButtonInfo);
        //当前小程序运行在企业微信环境中,返回设备信息
        wx.getSystemInfo({
            success: (res) => {
                const { statusBarHeight } = res
                //结构状态栏的高度，单位px 减去上面的得边距
                console.log(statusBarHeight)
                const margin = this.data.menuButtonInfo.top - statusBarHeight
                console.log(margin)
                this.setData({
                    //总上边距
                    navHeight: (this.data.menuButtonInfo.height + statusBarHeight + (margin * 2)),
                    searchMarginTop: statusBarHeight + margin, // 状态栏 + 胶囊按钮边距
                    searchHeight: this.data.menuButtonInfo.height,  // 与胶囊按钮同高
                    searchWidth: this.data.menuButtonInfo.right - this.data.menuButtonInfo.width, // 胶囊按钮右边坐标 - 胶囊按钮宽度 = 按钮左边可使用宽度
                    //   main:214
                })
            },
        })
        this.get();
        if (this.data.info.name !== '未登录') {
            this.setData({
                login: true
            })
        }
    },
    //打开详情
    open(event: any) {
        console.log(event.target.dataset.index)
        this.setData({
            open_index: event.target.dataset.index
        })
    },
    //预览图片
    openimage(event: any) {
    let now=event.currentTarget.dataset.all;
       now=now.split(',');
       if(now.length===1){
        let url=[];
         url.push(`${this.data.url}${now}`)
        wx.previewImage({
            current:url[0],// 当前显示图片的http链接
            urls:url// 需要预览的图片http链接列表
        })
       }else{
        let all = JSON.parse(event.currentTarget.dataset.all);
        const index = event.target.dataset.item;
        console.log(all)
        for (var i = 0; i < all.length; i++) {
            all[i] = this.data.url + all[i];
        }
        wx.previewImage({
            current: all[index],// 当前显示图片的http链接
            urls: all// 需要预览的图片http链接列表
        })
       }
        
       

    },
    //授权登录
    out() {
        const that = this
        wx.showModal({
            title: '提示',
            content: '是否授权登录',
            success(res) {
                if (res.confirm) {
                    var app = getApp();
                    app.login().then((res: number) => {
                        console.log(res)
                        that.setData({
                            info: app.globalData.info,
                            url: app.globalData.url,
                            status: app.globalData.status,
                            login: true
                        })
                    });

                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    back() {
        wx.switchTab({
            url: `/pages/my/my`

        })
    },
    get() {
        //文章评价
        wx.showLoading({
            title: '小的正在努力！',
          }) 
        var app = getApp();
        const that = this;
        console.log(this.data.info.openid)
        wx.request({
            url: `${app.globalData.url}api/comment?openid=${this.data.info.openid}&page=${this.data.page}`, //仅为示例，并非真实的接口地址
            success(res: { data: { code: number, data: { data: { id: number, news_image: string, news_name: string }[] }[] } }) {
                console.log(res);
                const info = res.data.data;
                console.log(info)
                console.log(res.data.code)
                //关掉提示加载
                wx.hideLoading();
                if (info) {
                    if (info.length < 5) {
                        that.setData({
                            again: true,
                        })
                    }
                    if (that.data.page === 1) {
                        console.log(1111)
                        that.setData({
                            mydiscussion: info
                        })
                    } else {
                        that.setData({
                            mydiscussion: that.data.mydiscussion.concat(info)
                        })
                    }
                } else {
                    console.log(222222)
                    that.setData({
                        mydiscussion: [],
                        again: true,
                    })
                }



            }

        })


    },
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        this.setData({
            page:this.data.page+1
        })
        this.get()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})